<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/10
 * Time: 10:55
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\admin\model\Car as Cars;
class Car extends Common
{
    public function car()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_display'] = array('eq',0);
            if (isset($pagedata['name']) && $pagedata['name'] != '') {
                $where['name'] = array('like', '%' . $pagedata['name'] . '%');
            }
            if (isset($pagedata['car_code']) && $pagedata['car_code'] != '') {
                $where['car_code'] = array('like', '%' . $pagedata['car_code'] . '%');
            }
            $app = new Cars();
            $count = $app->getCarCount($where);
            $data = $app->getCarList($where,$startrow,$pagesize);
            ajax_return(config('code.success_show'),'请求成功',$data,$count);

        } else {
            return view();
        }
    }

    public function delCar()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "car_id = {$post['id']}";
            $app = new Cars();
            $res = $app->carDel($where);
            if ($res) {
                ajax_return(0,'删除成功');
            } else {
                ajax_return(1,'删除失败');
            }
        }
    }
}